package com.idc.resource.controller;

import com.idc.resource.enums.AuthCodeEnum;
import com.idc.resource.enums.ErrorCodeEnum;
import com.idc.resource.exception.IdcServiceException;
import com.idc.resource.model.AuthCodeInfo;
import com.idc.resource.service.AuthCodeService;
import com.idc.resource.util.LogUtil;
import com.idc.resource.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import retrofit2.http.Body;

/**
 * 验证接口
 * Created by Administrator on 2017/7/16 0016.
 */
@Slf4j
@RestController
@RequestMapping(value = "/auth")
public class AuthController {

    /** 验证码认证服务 */
    private final AuthCodeService authCodeService;

    @Autowired
    public AuthController(AuthCodeService authCodeService) {
        this.authCodeService = authCodeService;
    }

    /**
     * 通过url链接点击验证
     * @param authType 验证码类型
     * @param authCodeInfo 验证请求参数
     * @return 验证结果
     */
    @RequestMapping(value = "/url/{authType}", method = RequestMethod.POST)
    public Result<Boolean> emailAuth(@PathVariable("authType") String authType,@Body AuthCodeInfo authCodeInfo){
        try {
            LogUtil.updateLogId(null);
            LogUtil.updateUserInfo(authCodeInfo.getUserId(),authCodeInfo.getToken());
            log.info("收到一个用户邮箱验证请求：{}",authCodeInfo);
//            return new Result<>(authCodeService.authCode(authType,authCodeInfo));
//            todo url get方式认证
            return null;
        }catch (IdcServiceException ise){
            log.error("用户邮箱验证失败,原因:{}",ise.getMessage());
            return new Result<>(ise.getCode(),ise.getMessage());
        }catch (Exception e){
            log.error("用户邮箱验证系统异常:",e);
            return new Result<>(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }

    /**
     * 邮箱验证
     * @param authCodeInfo 邮箱验证请求
     * @return 验证结果 true 验证成功
     */
    @RequestMapping(value = "/code/email", method = RequestMethod.POST)
    public Result<Boolean> emailAuth(@Body AuthCodeInfo authCodeInfo){
        try {
            LogUtil.updateLogId(null);
            LogUtil.updateUserInfo(authCodeInfo.getUserId(),authCodeInfo.getToken());
            log.info("收到一个用户邮箱验证请求：{}",authCodeInfo);
            return new Result<>(authCodeService.authCode(AuthCodeEnum.EMAIL,authCodeInfo));
        }catch (IdcServiceException ise){
            log.error("用户邮箱验证失败,原因:{}",ise.getMessage());
            return new Result<>(ise.getCode(),ise.getMessage());
        }catch (Exception e){
            log.error("用户邮箱验证系统异常:",e);
            return new Result<>(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }

    /**
     * 短信验证
     * @param authCodeInfo 短信验证请求
     * @return 验证结果 true 验证成功
     */
    @RequestMapping(value = "/code/msg", method = RequestMethod.POST)
    public Result<Boolean> msgAuth(@Body AuthCodeInfo authCodeInfo){
        try {
            LogUtil.updateLogId(null);
            LogUtil.updateUserInfo(authCodeInfo.getUserId(),authCodeInfo.getToken());
            log.info("收到一个用户短信验证请求：{}",authCodeInfo);
            return new Result<>(authCodeService.authCode(AuthCodeEnum.SMS_MSG,authCodeInfo));
        }catch (IdcServiceException ise){
            log.error("用户短信验证失败,原因:{}",ise.getMessage());
            return new Result<>(ise.getCode(),ise.getMessage());
        }catch (Exception e){
            log.error("用户短信验证系统异常:",e);
            return new Result<>(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }

    /**
     * 短信验证发送请求
     * @param authCodeInfo 短信验证发送
     * @return 发送结果 true 发送成功
     */
    @RequestMapping(value = "/send/msg", method = RequestMethod.POST)
    public Result<Boolean> sendMsg(@Body AuthCodeInfo authCodeInfo){
        try {
            LogUtil.updateLogId(null);
            log.info("收到一个手机验证短信验证码发送请求：{}",authCodeInfo);
            return new Result<>(authCodeService.sendAuthCode(AuthCodeEnum.SMS_MSG,authCodeInfo));
        }catch (IdcServiceException ise){
            log.error("手机验证短信验证码发送失败,原因:{}",ise.getMessage());
            return new Result<>(ise.getCode(),ise.getMessage());
        }catch (Exception e){
            log.error("手机验证短信验证码发送失败，系统异常:",e);
            return new Result<>(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }

    /**
     * 邮箱验证发送请求
     * @param authCodeInfo 邮箱验证发送
     * @return 发送结果 true 发送成功
     */
    @RequestMapping(value = "/send/email", method = RequestMethod.POST)
    public Result<Boolean> sendEmail(@Body AuthCodeInfo authCodeInfo){
        try {
            LogUtil.updateLogId(null);
            log.info("收到一个邮箱验证邮件验证码发送请求：{}",authCodeInfo);
            return new Result<>(authCodeService.sendAuthCode(AuthCodeEnum.EMAIL,authCodeInfo));
        }catch (IdcServiceException ise){
            log.error("邮箱验证邮件验证码发送失败,原因:{}",ise.getMessage());
            return new Result<>(ise.getCode(),ise.getMessage());
        }catch (Exception e){
            log.error("邮箱验证邮件验证码发送失败，系统异常:",e);
            return new Result<>(ErrorCodeEnum.SYSTEM_ERROR.getCode(),ErrorCodeEnum.SYSTEM_ERROR.getDesc());
        }
    }
}
